<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use fast\Random;
use phpCAS;
use think\Session;
use think\helper\Time;
use app\admin\model\Outsideschool as outside;
use think\Config;
use think\Db;
use app\admin\model\ReceptionUnit;
use think\Validate;

class Outsideschool2 extends Frontend
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';
    protected $layout = 'weiui';
    protected $model = null;

    public function _initialize(){
        parent::_initialize();
        $this->model = new outside();
    }

    /**
     * 平度校区入校
     * [index description]
     * @param  integer $ids [description]
     * @return [type]       [description]
     */
    public function index($ids = 1){
        if ($this->request->isPost()){
            $params = $this->request->post("row/a");
            //黑名单
            $re = $this->blacklist($params['nickname']);
            if (!$re){
                $this->error('你好，此用户不允许登录');
            }
            $params['create_time'] = time();
            $params['applier_id'] = 0;//申请人id
            $params['applier_name'] = '';//申请人名称
            $info = ReceptionUnit::get($params['reception_id']);
            $params['reception_name'] = $info['name'];
            if ($params['admission_starttime'])
                $params['admission_starttime'] = str_replace('/', '', $params['admission_starttime']);
            if ($params['admission_endtime'])
                $params['admission_endtime'] = str_replace('/', '', $params['admission_endtime']);
            if ($params['admission_endtime'] < $params['admission_starttime'])
                $this->error('入校截止时间不能小于入校开始时间');
            $last_month = strtotime('+3 day',strtotime($params['admission_starttime']));
            if(strtotime($params['admission_endtime'])>$last_month){
                $this->error('入校开始和截止时间间隔不可大于3天');
            }
	    $res = $this->model->allowField(true)->save($params);
            if ($res){
                if($info -> sid){
                        $sid_arr = explode(',',$info->sid) ;
                        foreach ($sid_arr as $key =>$v){
                            send_wechat_work_msg([$v], '<a href="https://yqsb.qau.edu.cn/index/outsideschool/view">您有一份校外人员'.$params['nickname'].'提交的入校预约申请需要审批。</a>');
                        }
                }
                $this->success('登记成功', '', ['nickname' => $params['nickname']]);
            }
            else{
                $this->error('登记失败');
            }
        }
        //接待单位
        $where = [];
        //测试用部门
        if (!input('admin_test')){
            $where['id'] = ['<>',1];
        }
        $where['id'] = 59;//必须是平度校区
        $reception_unit = ReceptionUnit::where($where)->field("name,id")->select();
        $this->assign('reception_unit', $reception_unit);

        $this->assign('ids', $ids);
        $title = '入校登记';
        $this->assign('pageTitle', $title);
        return $this->fetch('outsideschool/index2');
    }


    /*
     * 黑名单
     */
    public function blacklist($nickname){
        $user = [
            ['nickname' => '袁绍东'],
        ];
        foreach ($user as $k => $v)
        {
            if($v['nickname']==$nickname)
            {
                return false;
            }
        }
        return true;
    }

}
